//
//  HLLiveViewController.m
//  YueYue
//
//  Created by hulu001 on 2024/3/12.
//

#import "HLLiveViewController.h"

#import "HLZegoLiveKit.h"
#import "HLLiveOptionRequest.h"
#import "HLLiveInCallLayer.h"
#import "HLLiveResultViewController.h"

@interface HLLiveViewController ()<HLZegoLiveManagerDelegate,
HLLiveInCallLayerDelegate>

@property (nonatomic, strong) HLLiveFemaleModel *liveFemale;
@property (nonatomic, strong) UIView *localView;
@property (nonatomic, strong) HLLiveInCallLayer *liveInCallLayer;

@end

@implementation HLLiveViewController

- (instancetype)initWithLiveFemale:(HLLiveFemaleModel *)liveFemale
{
    self = [super init];
    if (self){
        self.liveFemale = liveFemale;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorBlack;
    [HLZegoLiveKit sharedInstance].delegate = self;
    [[[HLLiveOptionRequest alloc] initWithRoom:_liveFemale.liveRoomCode type:HLLiveOptionTypeJoin] startWithCompletionBlockWithSuccess:^(__kindof HLLiveOptionRequest * _Nonnull request) {
        if (request.isSuccess) {
            NSString *token = request.result[@"data"];
            [HLZegoLiveKit sharedInstance].tokenHandler = ^(uint64_t uid, void (^complete)(NSString *token, NSError *error)) {
                complete(token, nil);
            };
            [[HLZegoLiveKit sharedInstance] start];
            [[HLZegoLiveKit sharedInstance] joinWithFemale:self.liveFemale localView:self.localView];
            [self startLiveInCallLayer];
        } else {
            [QMUITips showWithText:@"直播间异常"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    UIApplication.sharedApplication.idleTimerDisabled = NO;
    [super viewDidDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    UIApplication.sharedApplication.idleTimerDisabled = YES;
    [super viewWillAppear:animated];
    
}
- (void)initSubviews
{
    [super initSubviews];
    self.localView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_localView];
    
}


- (void)startLiveInCallLayer
{
    self.liveInCallLayer = [[HLLiveInCallLayer alloc] initWithFemale:_liveFemale];
    _liveInCallLayer.delegate = self;
    [self.view addSubview:_liveInCallLayer];
    [_liveInCallLayer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (BOOL)preferredNavigationBarHidden
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (BOOL)shouldPopViewControllerByBackButtonOrPopGesture:(BOOL)byPopGesture
{
    return YES;
}
- (BOOL)forceEnableInteractivePopGestureRecognizer
{
    return NO;
}

- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view
{
    return YES;
}

#pragma mark - GPLiveCallingLayerDelegate

- (void)onLiveClose
{
    MJWeakSelf;
    [HLUIHelper showAlertWithTitle:@"温馨提示" content:@"确定离开直播间？" leftButtonTitle:@"我知道了" rightButtonTitle:@"确定" callBack:^(BOOL isConfirm) {
        if (isConfirm) {
            [HLGCDTimerManager cancel:ThumbTimeName];
            [[HLZegoLiveKit sharedInstance] leaveWithCallback:^(NSString *result) {
                if ([result isEqualToString:@"0"]) {
                    
                }
            }];
            [[[HLLiveOptionRequest alloc] initWithRoom:weakSelf.liveFemale.liveRoomCode type:HLLiveOptionTypeLeave] startWithCompletionBlockWithSuccess:^(__kindof HLLiveOptionRequest * _Nonnull request) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } failure:nil];
        }
    }];
    
    
}

#pragma mark - GPZegoLiveManagerDelegate


/// 进入房间的回调
/// @param users 接受者
- (void)live_onUserEnter:(NSArray<ZegoUser *> *)users
{
    [self.liveInCallLayer refreshCurrentUsers:users type:ZegoUpdateTypeAdd];
}

/// 离开房间的回调
/// @param users 接受者
- (void)live_onUserLeave:(NSArray<ZegoUser *> *)users
{
    [self.liveInCallLayer refreshCurrentUsers:users type:ZegoUpdateTypeDelete];
}


/// 直播结束
- (void)live_onCallEnd
{
    [self live_end];
}


- (void)live_onRecievedCustom:(NSString *)response user:(ZegoUser *)user
{
    [self.liveInCallLayer addCustomCommand:response user:user];
}

- (void)live_end
{
    [HLGCDTimerManager cancel:ThumbTimeName];
    [[HLZegoLiveKit sharedInstance] leaveWithCallback:^(NSString *result) {
        if ([result isEqualToString:@"0"]) {
            
        }
    }];
    [[[HLLiveOptionRequest alloc] initWithRoom:self.liveFemale.liveRoomCode type:HLLiveOptionTypeLeave] startWithCompletionBlockWithSuccess:^(__kindof HLLiveOptionRequest * _Nonnull request) {
        HLLiveResultViewController *liveResult = [[HLLiveResultViewController alloc] initWithLiveFemale:self.liveFemale];
        [self.navigationController pushViewController:liveResult animated:YES];
    } failure:nil];
}

@end
